package it.tukano.app;

import it.tukano.fun.Function1;

/**
 * A module of an application
 * @author pgi
 */
public interface AppModule {

    /**
     * Initializes this module
     * @param context the context of the application
     */
    void initModule(AppModule context);

    /**
     * Stars this module
     * @param context the context of the application
     */
    void startModule(AppModule context);

    /**
     * Stops this module
     * @param context the context of the application
     */
    void stopModule(AppModule context);

    /**
     * Destroyes this module
     * @param context the context of the application
     */
    void destroyModule(AppModule context);

    /**
     * Calls the find method of the context registered during initialization. This can be called after the invocation (by the framework) of the initializeModule method
     * (ie from postInit to destroyModule)
     * @param <A> the type of the module to get
     * @param moduleClass the module's class.
     * @return the first module of type B <: A found in the context of this module or null if no such module exists
     */
    <A extends AppModule> A find(Class<A> moduleClass);
    
    /**
     * Executes a callback with the result of find(moduleClass), if not null
     * @param <A> the type of the requested module
     * @param moduleClass the class of the module
     * @param callback the function to call
     */
    <A extends AppModule> void withModule(Class<A> moduleClass, Function1<A, ?> callback);
}